<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\ApiController;
use App\Libraries\Error;
use App\Models\Employee;
use App\Models\Salary;
use App\Models\User;
use Firebase\JWT\JWT;
use Illuminate\Http\Request;

class SalaryController extends ApiController
{


    public function create(Request $request) {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];


        if ( $request->id) {
            $salary = Salary::findOrFail($request->id);
            $salary->mtime = time();
        } else {
            $salary = new Salary();
            $salary->ctime = time();
        }
        $salary->employee_id = $request->employee_id;

        $employee = Employee::where('employee_id',$request->employee_id)->where('company_id',$company_id)->where('status',1)->first();
        $salary->company_id = $company_id;
        $salary->name = $employee['name'];
        $salary->organization_id = $employee['organization_id'];
        $salary->year_month = $request->year_month;
        $salary->working_hour_type = $request->working_hour_type;
        $salary->monthly_hours = $request->monthly_hours;
        $salary->actual_hours = $request->actual_hours;
        $salary->base_salary_probation_eriod = $request->base_salary_probation_eriod;
        $salary->post_salary_probation_eriod = $request->post_salary_probation_eriod;
        $salary->base_salary = $request->base_salary;
        $salary->post_salary = $request->post_salary;
        $salary->kpi_salary = $request->kpi_salary;
        $salary->traffic_salary = $request->traffic_salary;
        $salary->meal_supplement = $request->meal_supplement;
        $salary->quarterly_awards = $request->quarterly_awards;
        $salary->sales_commission = $request->sales_commission;
        $salary->piece_rate = $request->piece_rate;
        $salary->hourly_wage = $request->hourly_wage;
        $salary->seniority_pay = $request->seniority_pay;
        $salary->title_salary = $request->title_salary;
        $salary->attendance_deduction = $request->attendance_deduction;
        $salary->company_social_security = $request->company_social_security;
        $salary->wages_payable = $request->wages_payable;
        $salary->reissue_before_tax = $request->reissue_before_tax;
        $salary->supplementary_deduction_before_tax = $request->supplementary_deduction_before_tax;
        $salary->after_tax_reissue = $request->after_tax_reissue;
        $salary->commercial_insurance = $request->commercial_insurance;
        $salary->overtime_pay_working_days = $request->overtime_pay_working_days;
        $salary->overtime_rest_days = $request->overtime_rest_days;
        $salary->overtime_holidays = $request->overtime_holidays;
        $salary->total_overtime = $request->total_overtime;
        $salary->company_accumulation_fund = $request->company_accumulation_fund;
        $salary->no_post = $request->no_post;
        $salary->comprehensive_deduction = $request->comprehensive_deduction;
        $salary->total_attendance_deduction = $request->total_attendance_deduction;
        $salary->accumulated_revenue = $request->accumulated_revenue;
        $salary->accumulated_deduction_expenses = $request->accumulated_deduction_expenses;
        $salary->accumulated_special_deduction = $request->accumulated_special_deduction;
        $salary->accumulated_prepaid_tax = $request->accumulated_prepaid_tax;
        $salary->cumulative_child_education = $request->cumulative_child_education;
        $salary->accumulated_housing_rent = $request->accumulated_housing_rent;
        $salary->accumulative_support_elderly = $request->accumulative_support_elderly;
        $salary->accumulated_housing_loan_interest = $request->accumulated_housing_loan_interest;
        $salary->cumulative_continuing_education = $request->cumulative_continuing_education;
        $salary->accumulated_special_additional_deduction = $request->accumulated_special_additional_deduction;
        $salary->accumulated_taxable_income = $request->accumulated_taxable_income;
        $salary->accumulated_tax_payable = $request->accumulated_tax_payable;
        $salary->late_deduction = $request->late_deduction;
        $salary->early_leave_deduction = $request->early_leave_deduction;
        $salary->deduction_for_missing_card = $request->deduction_for_missing_card;
        $salary->deduction_absenteeism = $request->deduction_absenteeism;
        $salary->holiday_deduction = $request->holiday_deduction;
        $salary->person_social_security = $request->person_social_security;
        $salary->person_accumulation_fund = $request->person_accumulation_fund;
        $salary->children_education = $request->children_education;
        $salary->continue_education = $request->continue_education;
        $salary->house_loan = $request->house_loan;
        $salary->housing_rent = $request->housing_rent;
        $salary->tax = $request->tax;
        $salary->paid_wages = $request->paid_wages;
        $salary->remark = $request->remark;
        $salary->uid = $uid;

        $salary->save();

        return showError(Error::ERR_OK,['id'=>$salary->id]);

    }

    public function show(Request $request) {
        $salary = Salary::findOrFail($request->id);
        return showError(Error::ERR_OK,['salary'=>$salary]);
    }

    public function index(Request $request)
    {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];
        // 搜索内容
        $where = [];
        $name = $request->input('name');
        if (!empty($name))
        {
            $where['name'] = $name;
        }

        $organization_id = $request->input('organization_id');
        if (!empty($name))
        {
            $where['organization_id'] = explode(",",$organization_id);
        }


        $where['company_id'] = $company_id;
        $where['status'] = 1;

        $list = Salary::getList($where);
        foreach ($list as $k => &$v) {

            $v->employee_info = Employee::where('employee_id',$v->employee_id)->where('company_id',$company_id)->where('status',1)->first();
        }

        return showError(Error::ERR_OK,['list'=>$list]);
    }

    public function delete(Request $request) {
        Salary::where('id',$request->id)->update(['status'=>2,'mtime'=>time()]);
        return showError(Error::ERR_OK);
    }

    public function base(Request $request) {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
//        $company_id = $userInfo['company_id'];
        Salary::where('id',$request->id)->update(['base_salary'=>$request->base_salary,'mtime'=>time()]);
        return showError(Error::ERR_OK);
    }


}
